/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Actor.weapons;

import Actor.Actor;
import Actor.Projectile;
import actor.weapons.Explosion;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.Shape;
import java.awt.geom.Rectangle2D;
import javax.vecmath.Point2d;

/**
 *
 * @author Eric-Standard
 */
public class Grenade extends Projectile {

    private Point2d dest;
    private Rectangle r = new Rectangle();
    private final static int indicatorMaxRadius = 100;
    private final static int indicatorRadiusDelta = 1;
    private int indicatorRadius = 0;
    private static final int height = 10;
    private static final int width = 10;
    private double distanceTraveled;

    public Grenade() {
        super();
    }

    public Grenade(Point2d p) {
        super(p);
    }

    public Grenade(Point2d p, Point2d dest) {
        this(p);
        setDestination(dest);
    }

    public void setFuseTime(int fuseTime) {
        this.fuseTime = fuseTime;
    }

    public void setDestination(Point2d dest) {
        this.dest = dest;
        angle = Math.atan2(dest.y - pos.y, dest.x - pos.x);
        vel.x = Math.cos(angle);
        vel.y = Math.sin(angle);
        vel.scale(200);
    }

    @Override
    public void act(long dt) {
        super.act(dt);
        //Check if fuse is done
        if (fuseTime <= 0) {
            Explosion explosion = new Explosion(pos);
            explosion.setMaxRadius(100);
            world.add(explosion);
            removeSelfFromWorld();
        }
        //Update indicator
        indicatorRadius -= indicatorRadiusDelta;
        if (indicatorRadius < 0) {
            indicatorRadius = indicatorMaxRadius;
        }
        //Check if at destination
        if (pos != null && dest != null && pos.epsilonEquals(dest, 10)) {
            vel.x = 0;
            vel.y = 0;
        }
    }

    @Override
    public void draw(Graphics g) {
        g.setColor(Color.BLUE);
        g.fillOval((int) pos.x - width / 2, (int) pos.y - height / 2, width, height);
        g.setColor(Color.ORANGE);
        g.drawOval((int) pos.x - indicatorRadius, (int) pos.y - indicatorRadius, 2 * indicatorRadius, 2 * indicatorRadius);
    }

    @Override
    public void collide(Actor a) {
        if (owner == a) {
            return;
        }
        if (vel.lengthSquared() == 0) {
            return;
        }
        Rectangle2D rectA = a.getBoundingRectangle();
        if (pos.x > rectA.getMinX() && pos.x < rectA.getMaxX()) {
            vel.x = 0;
        }
        if (pos.y > rectA.getMinY() && pos.y < rectA.getMaxY()) {
            vel.y = 0;
        }
    }

    @Override
    public Shape getShape() {
        r.x = (int) pos.x;
        r.y = (int) pos.y;
        r.width = width;
        r.height = height;
        return r;
    }

    @Override
    public Rectangle2D getBoundingRectangle() {
        return r;
    }
}
